package com.luck.of.wise.somenslider.widget;

import java.util.Random;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ViewFlipper;

import com.luck.of.wise.somenslider.R;

public class SomenView extends LinearLayout {

    boolean mFlowing;
    boolean mCatchFlag;
    boolean mCatched;
    ImageView mSomenImageView;
    ImageView mUpImageView;
    AnimationDrawable mSomenAnimationDrawable;
    String mType;
    OnSomenListener mListener;

    AnimationListener mSomenAnimationListener = new AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            if (mListener != null) {
                if (!mCatched) {
                    Log.d("TEST----next:type", mType);
                    mListener.onFlowNext(mType);
                }
            }
            stopSomen();
        }
    };

    public SomenView(Context context) {
        super(context);
        init(context);
    }

    public SomenView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater factory = LayoutInflater.from(context);
        View v = factory.inflate(R.layout.somen_view, null);
        addView(v, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        setOnClickListener(mOnSomenClickListener);
        mSomenImageView = (ImageView) this.findViewById(R.id.somen_frame);
        mUpImageView = (ImageView) this.findViewById(R.id.up);

        ViewFlipper hashi = (ViewFlipper) this.findViewById(R.id.hashi);
        hashi.setOnClickListener(mOnHashiClickListener);
    }

    OnClickListener mOnSomenClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            flowSomen();
        }
    };

    OnClickListener mOnHashiClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mCatchFlag || (!mCatchFlag && mFlowing)) {
                if (mType.equals("0")) {
                    mUpImageView.setImageResource(R.drawable.soumen_android_image);
                } else {
                    mUpImageView.setImageResource(R.drawable.hashi_up);
                }
                ViewFlipper vf = (ViewFlipper) SomenView.this.findViewById(R.id.hashi);
                vf.showNext();
                mCatchFlag = !mCatchFlag;
                mCatched = true;
            }
            if (mCatchFlag) {
                stopSomen();
            }
        }
    };

    public void flowSomen() {
        Random rnd = new Random();
        int ran = rnd.nextInt(5);
        flowSomen(String.valueOf(ran));
    }

    public void flowSomen(String type) {
        if (mFlowing) {
            return;
        }
        mFlowing = true;
        mCatched = false;
        mType = type;
        if (mType.equals("0")) {
            mSomenImageView.setBackgroundResource(R.anim.somen_frame_anim0);
        } else {
            mSomenImageView.setBackgroundResource(R.anim.somen_frame_anim);
        }
        mSomenImageView.setVisibility(View.VISIBLE);
        if (mSomenImageView.getAnimation() != null) {
            return;
        }

        Animation anim = AnimationUtils.loadAnimation(this.getContext(), R.anim.somen_anim);
        anim.setAnimationListener(mSomenAnimationListener);
        mSomenImageView.setAnimation(anim);
        mSomenAnimationDrawable = (AnimationDrawable) mSomenImageView.getBackground();
    }

    public void stopSomen() {
        mSomenImageView.setVisibility(View.GONE);
        if (mSomenImageView.getAnimation() != null) {
            mSomenImageView.clearAnimation();
        }
        mSomenAnimationDrawable.stop();
        mFlowing = false;
    }

    public void setOnSomenListener(OnSomenListener listener) {
        mListener = listener;
    }

    public interface OnSomenListener {
        void onFlowNext(String type);
    }

}
